package io.renren.modules.message.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.message.entity.MessageEntity;
import io.renren.modules.message.service.MessageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 
 * 
 * @author wxy
 * @email sharonwang_xy@163.com
 * @date 2018-10-25 18:57:10
 */
@RestController
@RequestMapping("message/message")
public class MessageController {
	@Autowired
	private MessageService messageService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("message:message:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<MessageEntity> messageList = messageService.queryList(query);
		int total = messageService.queryList(query.noLimit()).size();
		
		PageUtils pageUtil = new PageUtils(messageList, total, query.getLimit(), query.getCurrPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("message:message:info")
	public R info(@PathVariable("id") Integer id){
		MessageEntity message = messageService.queryObject(id);
		
		return R.ok().put("message", message);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("message:message:save")
	public R save(@RequestBody MessageEntity message){

	    //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		message.setCreatedate(new Date());

        if (message.getType().equals("1")){        //类型为推送单条消息，添加并发送短信(标为已读)
            message.setReadstate(1);

            String url= "http\\://127.0.0.1\\:8090/PushMsg"+"/pushinfo/pushsingle";
            List<String> tempList = new ArrayList<>();
            tempList.add(message.getUsername());
            Map<String, Object> map = new HashMap<>();
            map.put("title", message.getTitle());
            map.put("content", message.getMessage());
            map.put("accountList", tempList);
//            String resultStr = new HystrixPostUtil(url, map).execute(); //TODO 去掉，集成到一个项目中或者单独开一个服务
            //RespModel respModel = JSON.parseObject(resultStr, RespModel.class);

        }else if (message.getType().equals("2")) {   //类型为推送全部设备
            message.setReadstate(1);
            message.setUsername("全部设备");
            String url = "http\\://127.0.0.1\\:8090/PushMsg" + "/pushinfo/pushalldevice";
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", message.getTitle());
            map.put("content", message.getMessage());
//            String resultStr = new HystrixPostUtil(url, map).execute();  //TODO
//            RespModel respModel = JSON.parseObject(resultStr, RespModel.class);
//            if (respModel.getStatus().equals("SUCCESS")) {
//                logger.info("推送服务已经收到了请求！");
//            } else {
//                logger.info("推送服务未成功收到请求！");
//            }
        }else if (message.getType().equals("0")){    //类型为普通消息
            message.setReadstate(0);
        }
        // 已读状态设置为未读
        message.setReadstate(0);

		messageService.save(message);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("message:message:update")
	public R update(@RequestBody MessageEntity message){

		messageService.update(message);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("message:message:delete")
	public R delete(@RequestBody Integer[] ids){
		messageService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
